
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class Search extends Canvas implements Runnable, CommandListener
{ 
    private Image wall;
    private Command search = new Command("Tim kiem", Command.ITEM, 0);
    private Command back = new Command("Tro lai", Command.BACK, 0);
    
   
    
    

int clearKeyCode = Integer.MIN_VALUE;

StringBuffer currentText = new StringBuffer();
String currentString = new String();

int lastPressedKey = Integer.MIN_VALUE;
int currentKeyStep = 0;

Font inputFont = null;
int inputWidth = 0;
int inputHeight = 0;
int inputTranslationX = 0;

long lastKeyTimestamp = 0;
long maxKeyDelay = 500L;

int caretIndex = 0;
int caretLeft = 9;
boolean caretBlinkOn = true;
long caretBlinkDelay = 500L;
long lastCaretBlink = 0;

boolean goToNextChar = true;

static final char[] KEY_NUM1_CHARS = {'.', ',', '?', ';', '!', '1'};
static final char[] KEY_NUM2_CHARS = {'a', 'b', 'c', '2'};
static final char[] KEY_NUM3_CHARS = {'d', 'e', 'f', '3'};
static final char[] KEY_NUM4_CHARS = {'g', 'h', 'i', '4'};
static final char[] KEY_NUM5_CHARS = {'j', 'k', 'l', '5'};
static final char[] KEY_NUM6_CHARS = {'m', 'n', 'o', '6'};
static final char[] KEY_NUM7_CHARS = {'p', 'q', 'r', 's', '7'};
static final char[] KEY_NUM8_CHARS = {'t', 'u', 'v', '8'};
static final char[] KEY_NUM9_CHARS = {'w', 'x', 'y', 'z', '9'};
static final char[] KEY_NUM0_CHARS = {' '};
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);


public Search()
{
    this.addCommand(search);
    this.addCommand(back);
    this.setCommandListener(this);
        try {
            wall = Image.createImage("/walls.png");
            
        } catch (Exception e){
        throw new RuntimeException("Loi tai hinh anh: "+e);
        }
this.setTitle("Tim kiem ban be");
new Thread(this).start();
inputFont = Font.getDefaultFont();
inputWidth = getWidth();
inputHeight = inputFont.getHeight();
}

public char[] getChars(int key)
{
switch(key)
{
case Canvas.KEY_NUM1: return KEY_NUM1_CHARS;
case Canvas.KEY_NUM2: return KEY_NUM2_CHARS;
case Canvas.KEY_NUM3: return KEY_NUM3_CHARS;
case Canvas.KEY_NUM4: return KEY_NUM4_CHARS;
case Canvas.KEY_NUM5: return KEY_NUM5_CHARS;
case Canvas.KEY_NUM6: return KEY_NUM6_CHARS;
case Canvas.KEY_NUM7: return KEY_NUM7_CHARS;
case Canvas.KEY_NUM8: return KEY_NUM8_CHARS;
case Canvas.KEY_NUM9: return KEY_NUM9_CHARS;
case Canvas.KEY_NUM0: return KEY_NUM0_CHARS;
}
return null;
}
boolean isClearKey(int key)
{
return key == -8;
}

void clearChar()
{
if(currentText.length() > 0 && caretIndex > 0)
{
caretIndex--;

currentText.deleteCharAt(caretIndex);

currentString = currentText.toString();
}
}
void updateCaretPosition()
{
caretLeft = inputFont.substringWidth(currentString, 0, caretIndex)+9;

if(caretLeft + inputTranslationX < 0)
{
inputTranslationX = - caretLeft;
}
else if(caretLeft + inputTranslationX > inputWidth)
{
inputTranslationX = inputWidth - caretLeft;
}
}
public void keyPressed(int keyCode)
{
    int gameAction = getGameAction(keyCode);

System.out.println("KEY: " + keyCode + ", " + gameAction);

if(isClearKey(keyCode))
{
clearChar();

updateCaretPosition();

goToNextChar = true;
}
else if(keyCode >= KEY_NUM0 && keyCode <= KEY_NUM9)
{
if(currentString.length()<20)
writeKeyPressed(keyCode);
}
else if(gameAction == Canvas.LEFT)
{
if(caretIndex > 0)
{
caretIndex--;

updateCaretPosition();

goToNextChar = true;
}
}
else if(gameAction == Canvas.RIGHT)
{
if(caretIndex < currentText.length())
{
if(goToNextChar)
caretIndex++;

updateCaretPosition();

goToNextChar = true;
}
}
}
public void writeKeyPressed(int key)
{
if(goToNextChar || key != lastPressedKey)
{
goToNextChar = true;

lastPressedKey = key;

currentKeyStep = 0;
}
else
{
currentKeyStep++;
}

char[] chars = getChars(key);

if(chars != null)
{
if(currentKeyStep >= chars.length)
{
currentKeyStep -= chars.length;
}
if(goToNextChar)
{
currentText.insert(caretIndex, chars[currentKeyStep]);

caretIndex++;
}
else
{
currentText.setCharAt(caretIndex - 1, chars[currentKeyStep]);
}
currentString = currentText.toString();

updateCaretPosition();

lastKeyTimestamp = System.currentTimeMillis();

goToNextChar = false;
}
}

public void checkTimestamps()
{
long currentTime = System.currentTimeMillis();

if(lastCaretBlink + caretBlinkDelay < currentTime)
{
caretBlinkOn = !caretBlinkOn;

lastCaretBlink = currentTime;
}

if(!goToNextChar && lastKeyTimestamp + maxKeyDelay < currentTime)
{
goToNextChar = true;
}
}

public void run()
{
while(true)
{
checkTimestamps();

repaint();

try
{
synchronized(this)
{
wait(50L);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

 

public void paint(Graphics g)
{

g.setColor(0xffffff);

g.fillRect(0, 0, getWidth(), getHeight());

g.drawImage(wall, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);g.drawImage(wall, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);g.drawImage(wall, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);


g.setColor(0x000000);

g.setFont(font);

g.drawString("Nhap noi dung:", 6, 16, Graphics.LEFT | Graphics.TOP);

g.setColor(0xff1493);

g.drawRect(5,35, 119,20);

g.setColor(0xff0000);

g.translate(inputTranslationX, 0);

g.setFont(inputFont);

System.out.println("Chuoi hien tai"+currentString);



if(caretBlinkOn && goToNextChar)
{
g.drawLine(caretLeft, 50,caretLeft, inputHeight+25);
}

g.translate(- inputTranslationX, 0);

g.drawString(currentString, 8, 39, Graphics.LEFT | Graphics.TOP);


}

    public void commandAction(Command c, Displayable d) {
        
        if (c == search) {
            
            //
            
        }
        
        else if (c == back) {
            SetDisplay.setCurrent(SetDisplay.FRIENDSLIST);
        }
        
    }
}



 

   
